package com.uls.web.controller.ycl;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletResponse;

import com.uls.common.core.domain.entity.SysUser;
import com.uls.system.service.impl.SysUserServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.ycl.domain.RepairPersonnel;
import com.uls.ycl.service.IRepairPersonnelService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;

/**
 * 维修人员Controller
 * 
 * @author ycl
 * @date 2024-11-14
 */
@RestController
@RequestMapping("/ycl/personnel")
public class RepairPersonnelController extends BaseController
{
    @Autowired
    private IRepairPersonnelService repairPersonnelService;

    @Autowired
    private SysUserServiceImpl sysUserService;
    /**
     * 查询维修人员列表
     */
    @PreAuthorize("@ss.hasPermi('ycl:personnel:list')")
    @GetMapping("/list")
    public TableDataInfo list(RepairPersonnel repairPersonnel)
    {
        startPage();
        List<RepairPersonnel> list = repairPersonnelService.selectRepairPersonnelList(repairPersonnel);

        for(RepairPersonnel personnel :list){
            Long userId = personnel.getUserId();

            SysUser sysUser = sysUserService.selectUserById(userId);
            if(sysUser!=null)
                personnel.setUserName(sysUser.getUserName());

        }

        return getDataTable(list);
    }

    /**
     * 导出维修人员列表
     */
    @PreAuthorize("@ss.hasPermi('ycl:personnel:export')")
    @Log(title = "维修人员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RepairPersonnel repairPersonnel)
    {
        List<RepairPersonnel> list = repairPersonnelService.selectRepairPersonnelList(repairPersonnel);
        ExcelUtil<RepairPersonnel> util = new ExcelUtil<RepairPersonnel>(RepairPersonnel.class);
        util.exportExcel(response, list, "维修人员数据");
    }

    /**
     * 获取维修人员详细信息
     */
    @PreAuthorize("@ss.hasPermi('ycl:personnel:query')")
    @GetMapping(value = "/{personnelId}")
    public AjaxResult getInfo(@PathVariable("personnelId") Long personnelId)
    {
        return success(repairPersonnelService.selectRepairPersonnelByPersonnelId(personnelId));
    }

    /**
     * 新增维修人员
     */
    @PreAuthorize("@ss.hasPermi('ycl:personnel:add')")
    @Log(title = "维修人员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RepairPersonnel repairPersonnel)
    {
        String phone = repairPersonnel.getPhone();

        // 正则表达式，匹配中国大陆的手机号码
        String regex = "^1[3-9]\\d{9}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(phone);

        if (matcher.matches()) {
            // 电话号码格式正确
            return toAjax(repairPersonnelService.insertRepairPersonnel(repairPersonnel));
        } else {
            // 电话号码格式不正确
            return AjaxResult.error("电话号码格式不正确");
        }

    }

    /**
     * 修改维修人员
     */
    @PreAuthorize("@ss.hasPermi('ycl:personnel:edit')")
    @Log(title = "维修人员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RepairPersonnel repairPersonnel)
    {
        String phone = repairPersonnel.getPhone();

        // 正则表达式，匹配中国大陆的手机号码
        String regex = "^1[3-9]\\d{9}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(phone);

        if (matcher.matches()) {
            // 电话号码格式正确
            return toAjax(repairPersonnelService.updateRepairPersonnel(repairPersonnel));
        } else {
            // 电话号码格式不正确
            return AjaxResult.error("电话号码格式不正确");
        }
    }

    /**
     * 删除维修人员
     */
    @PreAuthorize("@ss.hasPermi('ycl:personnel:remove')")
    @Log(title = "维修人员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{personnelIds}")
    public AjaxResult remove(@PathVariable Long[] personnelIds)
    {
        return toAjax(repairPersonnelService.deleteRepairPersonnelByPersonnelIds(personnelIds));
    }
}
